import moment from "moment";

export function formatTime(time, f) {
  const format = f || 'YYYY/MM/DD HH:mm:ss'
  time = moment(time).isValid() ? time : Number(time)

  return moment(time).format(format)
}

export function extractKeyword(str) {
  let keywords = []
  if (typeof str !== 'string') return keywords
  str = str.trim()
  if (!str) return keywords
  str.split()
}

export function cached(fn) {
  const cache = Object.create(null);
  return (async function cachedFn(str) {
    const hit = cache[str]
    return hit || (cache[str] = await fn(str))
  })
}

export function canUpdateCached(fn) {
  const cache = Object.create(null)
  return [
    (async function cachedFn(str) {
      const hit = cache[str]
      return hit || (cache[str] = await fn(str))
    }),
    (async function updatedFn(str) {
      return (cache[str] = await fn(str))
    })
  ]
}
